package com.harmony.pilot.data.controller;

import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.data.service.OpenHarmonyComponentService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * OpenHarmony组件查询控制器
 * 提供组件识别和API查询功能
 */
@Slf4j
@RestController
@RequestMapping("/data/openharmony/component")
@RequiredArgsConstructor
public class OpenHarmonyComponentController {
    
    private final OpenHarmonyComponentService componentService;
    
    /**
     * 识别用户消息中的OpenHarmony组件
     */
    @PostMapping("/identify")
    public RestResult<List<OpenHarmonyComponentService.ComponentMatch>> identifyComponents(
            @RequestBody ComponentIdentifyRequest request) {
        
        log.info("Identifying components from message: {}", request.getMessage());
        
        List<OpenHarmonyComponentService.ComponentMatch> matches = 
            componentService.identifyComponents(request.getMessage());
        
        return RestResult.success(matches);
    }
    
    /**
     * 查询组件的API列表
     */
    @PostMapping("/apis")
    public RestResult<List<OpenHarmonyComponentService.ComponentAPI>> queryComponentAPIs(
            @RequestBody ComponentAPIRequest request) {
        
        log.info("Querying APIs for component: {} (capability: {})", 
            request.getComponentName(), request.getCapability());
        
        List<OpenHarmonyComponentService.ComponentAPI> apis = 
            componentService.queryComponentAPIs(
                request.getComponentName(), 
                request.getCapability()
            );
        
        return RestResult.success(apis);
    }
    
    /**
     * 分析跨模块调用
     */
    @PostMapping("/cross-module-calls")
    public RestResult<List<OpenHarmonyComponentService.CrossModuleCall>> analyzeCrossModuleCalls(
            @RequestBody CrossModuleCallRequest request) {
        
        log.info("Analyzing cross-module calls for: {}", request.getFilePath());
        
        List<OpenHarmonyComponentService.CrossModuleCall> calls = 
            componentService.analyzeCrossModuleCalls(
                request.getFilePath(),
                request.getFunctionName()
            );
        
        return RestResult.success(calls);
    }
    
    /**
     * 根据用户需求推荐组件和API
     */
    @PostMapping("/recommend")
    public RestResult<ComponentRecommendation> recommendComponent(
            @RequestBody ComponentRecommendRequest request) {
        
        log.info("Recommending component for: {}", request.getRequirement());
        
        // 1. 识别组件
        List<OpenHarmonyComponentService.ComponentMatch> matches = 
            componentService.identifyComponents(request.getRequirement());
        
        if (matches.isEmpty()) {
            return RestResult.error("未识别到相关组件");
        }
        
        // 2. 查询API
        OpenHarmonyComponentService.ComponentMatch bestMatch = matches.get(0);
        List<OpenHarmonyComponentService.ComponentAPI> apis = 
            componentService.queryComponentAPIs(
                bestMatch.getComponentName(),
                request.getRequirement()
            );
        
        // 3. 构建推荐结果
        ComponentRecommendation recommendation = new ComponentRecommendation();
        recommendation.setComponentName(bestMatch.getComponentName());
        recommendation.setComponentDescription(bestMatch.getComponentInfo().getDescription());
        recommendation.setConfidence(bestMatch.getScore());
        recommendation.setApis(apis);
        recommendation.setRecommendedAPI(apis.isEmpty() ? null : apis.get(0));
        
        return RestResult.success(recommendation);
    }
    
    @Data
    public static class ComponentIdentifyRequest {
        private String message;
    }
    
    @Data
    public static class ComponentAPIRequest {
        private String componentName;
        private String capability;
    }
    
    @Data
    public static class CrossModuleCallRequest {
        private String filePath;
        private String functionName;
    }
    
    @Data
    public static class ComponentRecommendRequest {
        private String requirement;
    }
    
    @Data
    public static class ComponentRecommendation {
        private String componentName;
        private String componentDescription;
        private double confidence;
        private List<OpenHarmonyComponentService.ComponentAPI> apis;
        private OpenHarmonyComponentService.ComponentAPI recommendedAPI;
    }
}

